﻿using BaseDll;
using CommonTools;
using Entity;
using FinalTest_Algonrithm;
using FinalTest_Algonrithm.Clibra;
using StationDemo.自定义界面;
using Sunny.UI;
using System;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using UserData;

namespace StationDemo
{
    public partial class Form_Manual转盘 : UIPage
    {

        private AutoSizeFormClass asc = new AutoSizeFormClass();

        string strStationName = "";


        bool isCaping = false;

        public Form_Manual转盘(string name)
        {
            strStationName = name;
            InitializeComponent();
            asc.controllInitializeSize(this);
        }

        private void Function_遍历控件(Control control)
        {

            foreach (var item in control.Controls)
            {
                if (item is Interface_UserControl)
                {
                    ((Interface_UserControl)item).Init();
                }
                else
                {
                    Function_遍历控件((Control)item);
                }
            }
        }
        private void btn_顺时针旋转_Click(object sender, EventArgs e)
        {
            if (UIMessageBox.ShowAsk("是否确认治具A回到上料站"))
            {
                Station_Table station_Table = (Station_Table)StationMgr.GetInstance().GetStation(Station.转盘站.ToString());
                station_Table.Function_TurnTable(Station.上料站);
            }
        }

        private void btn_逆时针旋转_Click(object sender, EventArgs e)
        {
            if (UIMessageBox.ShowAsk("是否确认治具B回到上料站"))
            {
                Station_Table station_Table = (Station_Table)StationMgr.GetInstance().GetStation(Station.转盘站.ToString());
                station_Table.Function_TurnTable(Station.标定站);
            }
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            Station_Load station_Load = (Station_Load)StationMgr.GetInstance().GetStation(Station.上料站.ToString());
            //station_Load.Function_顶升_伸出(true);
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            Station_Load station_Load = (Station_Load)StationMgr.GetInstance().GetStation(Station.上料站.ToString());
            station_Load.Function_回退_下降(true);
        }


        private void btn_Stop_Click(object sender, EventArgs e)
        {
            LogHelper.OnTest_ShowLog("手动界面日志", $"停止");
            int socketnum = chk_治具号.SelectedIndex;

            Station_Load station_load = (Station_Load)StationMgr.GetInstance().GetStation(Station.上料站.ToString());
            string capBoxSN = "";
            if (socketnum <= 0)
            {
                Station_Table station_Table = (Station_Table)StationMgr.GetInstance().GetStation(Station.转盘站.ToString());
                capBoxSN = station_Table.clibraStation.socket == SOCKET.SOCKETA ? station_load.station_Config.str_StationACapBoxSN : station_load.station_Config.str_StationBCapBoxSN;
            }
            if (socketnum == 1)
            {
                capBoxSN = station_load.station_Config.str_StationACapBoxSN;
            }
            if (socketnum == 2)
            {
                capBoxSN = station_load.station_Config.str_StationBCapBoxSN;
            }
            station_load.Function_CapImg(capBoxSN, true, out string msga);
            isCaping = false;

        }

        private void btn_点亮_Click(object sender, EventArgs e)
        {
            int socketnum = chk_治具号.SelectedIndex;

            Station_Load station_load = (Station_Load)StationMgr.GetInstance().GetStation(Station.上料站.ToString());
            string capBoxSN = "";
            if (socketnum <= 0)
            {
                Station_Table station_Table = (Station_Table)StationMgr.GetInstance().GetStation(Station.转盘站.ToString());
                capBoxSN = station_Table.clibraStation.socket == SOCKET.SOCKETA ? station_load.station_Config.str_StationACapBoxSN : station_load.station_Config.str_StationBCapBoxSN;
            }
            if (socketnum == 1)
            {
                capBoxSN = station_load.station_Config.str_StationACapBoxSN;
            }
            if (socketnum == 2)
            {
                capBoxSN = station_load.station_Config.str_StationBCapBoxSN;
            }



            bool v = station_load.Function_CapImg(capBoxSN, false, out string msg);
            if (!v)
            {
                UIMessageBox.ShowError($"{msg}");
                return;
            }
            isCaping = true;
            Task.Factory.StartNew(() =>
            {
                while (isCaping)
                {
                    Bitmap bmp = station_load.Function_GetBMP(capBoxSN);
                    if (bmp != null)
                    {
                        defineVisionImage1.ShowVisionImage("BD", bmp, bmp);
                    }
                    else
                    {
                        UIMessageBox.ShowError("cap bmp error");
                        station_load.Function_CapImg(capBoxSN, true, out string msga);
                        isCaping = false;
                    }
                }
            });
        }

        private void btn_取图_Click(object sender, EventArgs e)
        {
            string path = $"D:\\HY System\\结果\\手动图片\\{DateTime.Now.ToString("yyyyMMdd")}\\{DateTime.Now.ToString("yyyyMMddHHssmmfff")}.bmp";
            if (!Directory.Exists(Path.GetDirectoryName(path)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(path));
            }

            System.Collections.Generic.Dictionary<object, Image> dic_Image = VisionBaseLib.VisionBaseMgr.GetInstance().dic_Image;
            dic_Image["BD"].Save(path,System.Drawing.Imaging.ImageFormat.Bmp);
        }

        private void btn_烧录_Click(object sender, EventArgs e)
        {

        }



        private void btn_测试_Click(object sender, EventArgs e)
        {

            string configPath = HuayaLibCalib.ConfigPath(ParamSetMgr.GetInstance().CurrentProductFile);
            int v = HuayaLibCalib.hy_load_configs(configPath, 1);
            if (v != 1)
            {
                UIMessageBox.ShowError($"hy_load_configs error api:{v}");
                return;
            }

            Station_Load station_load = (Station_Load)StationMgr.GetInstance().GetStation(Station.上料站.ToString());
            int socketnum = chk_治具号.SelectedIndex;
            string capBoxSN = "";
            if (socketnum == 0)
            {
                Station_Table station_Table = (Station_Table)StationMgr.GetInstance().GetStation(Station.转盘站.ToString());
                capBoxSN = station_Table.clibraStation.socket == SOCKET.SOCKETA ? station_load.station_Config.str_StationACapBoxSN : station_load.station_Config.str_StationBCapBoxSN;
            }
            if (socketnum == 1)
            {
                capBoxSN = station_load.station_Config.str_StationACapBoxSN;
            }
            else
            {
                capBoxSN = station_load.station_Config.str_StationBCapBoxSN;
            }
            Bitmap bmp = station_load.Function_GetBMP(capBoxSN);
            byte[] srcImageByteBuffer = ModuleCaptureMgr.ConvertBitmapToByteArray(bmp);
            byte[] destImageByteBuffer = new byte[srcImageByteBuffer.Length];
            unsafe
            {
                fixed (byte* srcImageBufferPtr = srcImageByteBuffer)
                {
                    fixed (byte* destImageByteBufferPtr = destImageByteBuffer)
                    {
                        int v2 = HuayaLibCalib.hy_get_corners(0, 0, srcImageBufferPtr, true, destImageByteBufferPtr);
                        Bitmap destbmp = ModuleCaptureMgr.ConvertBinaryToBitmap(destImageByteBuffer, station_load.station_Param.出图图像宽._strParamVal, station_load.station_Param.出图图像高._strParamVal);
                        VisionBaseLib.VisionBaseMgr.GetInstance().OnTest_ShowImage("Conner", destbmp, destbmp);
                    }
                }
            }

            double ReproErr = 0;
            double Cx = 0;
            double Cy = 0;
            double Fx = 0;
            double Fy = 0;
            double[] K = new double[4];
            int nResult = HuayaLibCalib.hy_start_calib_cv_fisheye(0, ref ReproErr, ref Cx, ref Cy, ref Fx, ref Fy, K);
            string ResultString = string.Format($"Result: {nResult}\n");
            ResultString = string.Format($"{ResultString} ReproErr: {ReproErr}\n");
            ResultString = string.Format($"{ResultString} Cx: {Cx}\n");
            ResultString = string.Format($"{ResultString} Cy: {Cy}\n");
            ResultString = string.Format($"{ResultString} Fx: {Fx}\n");
            ResultString = string.Format($"{ResultString} Fy: {Fy}\n");
            ResultString = string.Format($"{ResultString} K1: {K[0]}\n");
            ResultString = string.Format($"{ResultString} K2: {K[1]}\n");
            ResultString = string.Format($"{ResultString} K3: {K[2]}\n");
            ResultString = string.Format($"{ResultString} K4: {K[3]}\n");

            LogHelper.OnTest_ShowLog("手动界面日志", ResultString);

        }



        private void Form_Manual转盘_Load(object sender, EventArgs e)
        {

            Function_遍历控件(this);

        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            Station_Table station = (Station_Table)StationMgr.GetInstance().GetStation(Station.转盘站.ToString());
            //station.Function_保存数据();
        }

        private void uiButton1_Click_3(object sender, EventArgs e)
        {

        }

        private void uiButton1_Click_1(object sender, EventArgs e)
        {
            FileDialog fileDialog = new OpenFileDialog();
            fileDialog.Filter = "BMP 图片|*.bmp";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string configPath = HuayaLibCalib.ConfigPath(ParamSetMgr.GetInstance().CurrentProductFile);
                int v = HuayaLibCalib.hy_load_configs(configPath, 1);

                string fileName = fileDialog.FileName;
                using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    Bitmap image = (Bitmap)Image.FromStream(fileStream);
                    defineVisionImage1.ShowVisionImage("BD", image, image);
                }
            }
        }

        private void Form_Manual转盘_Resize(object sender, EventArgs e)
        {
            this.asc.controlAutoSize(this);
        }

        private void uiButtonChartCenter_Click(object sender, EventArgs e)
        {
            Station_标定站 station_Clibra = (Station_标定站)StationMgr.GetInstance().GetStation(Station.标定站.ToString());
            station_Clibra.Function_ChartCenter(out string msg, out Bitmap destBmp, true);
            VisionBaseLib.VisionBaseMgr.GetInstance().OnTest_ShowImage("Conner", destBmp, destBmp);
        }

        private void uiButton2_Click_1(object sender, EventArgs e)
        {
            Station_标定站 station_Clibra = (Station_标定站)StationMgr.GetInstance().GetStation(Station.标定站.ToString());
            station_Clibra.Function_计算比例(out string msg);
            LogHelper.OnTest_ShowLog("手动界面日志", msg);
        }

        private void uiButton3_Click_1(object sender, EventArgs e)
        {
            Station_标定站 station_Clibra = (Station_标定站)StationMgr.GetInstance().GetStation(Station.标定站.ToString());
            station_Clibra.Function_针孔模型6K标定测试(out string msg, true);
            LogHelper.OnTest_ShowLog("手动界面日志", msg);
        }

        private void uiButton4_Click(object sender, EventArgs e)
        {
            LoadSeverMgr.Instance.Start();
        }

        private void uiButton5_Click(object sender, EventArgs e)
        {
            LoadSeverMgr.Instance.Stop();
        }

        private void uiButton6_Click(object sender, EventArgs e)
        {
            string text = uiComboBox1.Text;
            if(string.IsNullOrEmpty(text))
            {
                return;
            }

            int index = Convert.ToInt32(text);

            LoadSeverMgr.Instance.Function_取图(LoadSeverMgr.Instance.server1, index);
        }

        private void uiButton7_Click(object sender, EventArgs e)
        {
            double[] value = new double[64];
            LoadSeverMgr.Instance.Function_标定计算(LoadSeverMgr.Instance.server1,out  value);
        }
    }
}
